//package com.cc.police.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.cc.police.config.LoginRequired;
//import com.cc.police.entity.Cpu;
//import com.cc.police.entity.PPolice;
//import com.cc.police.entity.PUpdate;
//import com.cc.police.mapper.PPoliceMapper;
//import com.cc.police.mapper.PUpdateMapper;
//import com.cc.police.util.RedisUtil;
//import com.cc.police.util.excelUtil;
//import com.cc.police.util.Variable;
//import org.apache.commons.beanutils.BeanUtils;
//import org.apache.commons.lang.StringUtils;
//import org.influxdb.InfluxDB;
//import org.influxdb.dto.Point;
//import org.influxdb.dto.Query;
//import org.influxdb.dto.QueryResult;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.CrossOrigin;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import redis.clients.jedis.Jedis;
//
//import javax.xml.crypto.Data;
//import java.lang.reflect.InvocationTargetException;
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//
//
//@RestController
//@RequestMapping("/influx")
//@CrossOrigin//前后端请求跨域注解
//public class DemoController {
//    @Autowired
//    private InfluxDB influxDB;
//    @Autowired
//    PPoliceMapper mapper;
//    @Autowired
//    PUpdateMapper updateMapper;
//    @Autowired
//    RedisUtil redisUtil;
//
//
//
//
//
//
//
//
//
////        @Scheduled(fixedRate = 5000)
////        @RequestMapping("/time")
////        public void reportCurrentTime() {
////        //定时任务注解
////            System.out.println("现在时间：" +new Date());
////
////        }
//
//
//
//
//
////    @Scheduled(fixedRate = 5000)
//    @GetMapping("/get")
//    @LoginRequired(loginSuccess = true)
//    public String test(){
////        String sql = "select * from \"7S_2A_Current_Feed\"";
////        String sql = "select * from "+ "\"7S_2A_Current_Feed\"";
//        long date = new Date().getTime();
//        System.err.println(date);
//        List<Cpu> trackPoints = new ArrayList<>();
//
//        List data =  excelUtil.excel();
//        System.out.println("数据条数="+data.size());
//        System.out.println("集合数据="+data);
//
//        List<PUpdate> pUpdate = updateMapper.selectAll();
//        System.out.println("size= "+pUpdate.size());
////        System.out.println("shuju="+pUpdate);
//        System.out.println("cariable= "+pUpdate.get(0));
//        System.out.println("cariable= "+pUpdate.get(1));
//        System.out.println("cariable= "+pUpdate.get(2));
//        System.out.println("cariable= "+pUpdate.get(3));
//
//        for (int j = 0; j < data.size(); j++) {
////            System.out.println("cariable= "+pUpdate.get(j));
////            String sql = "select * from "+ "\""+data.get(j)+"\"";
//            String sql = "select * from "+ "\""+data.get(j)+"\""+" WHERE time > now() - 5m";
//
//
//            System.err.println("sql语句"+sql);
//
//            Query query = new Query(sql,"gydb");
//            influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
//            //毫秒输出
//            QueryResult queryResult = influxDB.query(query, TimeUnit.MILLISECONDS);
//            List<QueryResult.Result> resultList =  queryResult.getResults();
//
//            String sss = queryResult.toString();
////        //两种方式都可以
////        InfluxDBConfig config = new InfluxDBConfig(userName,password,url,"bdcloud");
////        InfluxDB dbConfig = config.getInfluxDB();
////        queryResult = dbConfig.query(query);
////
//            //把查询出的结果集转换成对应的实体对象，聚合成list
//
//            for(QueryResult.Result result:resultList){
//                List<QueryResult.Series> seriesList = result.getSeries();
//                if(seriesList==null){
//                    break;
//                }else {
//                    for(QueryResult.Series series : seriesList){
//                        String name = series.getName();
//                        Map<String, String> tags = series.getTags();
//                        List<String> columns = series.getColumns();
//                        String[] keys =  columns.toArray(new String[columns.size()]);
//                        List<List<Object>> values = series.getValues();
//                        for(List<Object> value:values){
//                            Map beanMap = new HashMap();
//                            Cpu point = new Cpu();
//                            for (int i = 0; i < keys.length; i++) {
//                                beanMap.put(keys[i],value.get(i));
//                            }
//                            try {
//                                //查询的时候没用，写入point的时候用
//                                Point po = Point.measurement("trackpoint").fields(beanMap).build();
//                                //time 默认存入的是UTC格式  2020-04-29T22:58:23.58978834，默认转换成String
//                                //{"cpuid":"11","cputype":"H","lat":"10.111222","lon":"78.000111","state":"on","time":"2020-04-29T22:57:06.732701567Z"}
//                                BeanUtils.populate(point,beanMap );
//                            } catch (IllegalAccessException e) {
//                                e.printStackTrace();
//                            } catch (InvocationTargetException e) {
//                                e.printStackTrace();
//                            }
//
//                            //调用方法
////                        Variable.variable(data.get(j),point.getValue());
//                            variable(data.get(j),point.getValue());
//
////                        System.err.println(point.toString());
//                            trackPoints.add(point);
//                        }
//                    }
//                }
//
//            }
////            System.err.println("points:"+trackPoints.toString());
//
//        }
//        long date1 = new Date().getTime();
//        System.err.println("开始= "+date);
//        System.err.println("结束= "+date1);
//        System.err.println(date1-date);
//        return trackPoints.toString();
//
//    }
//
//
//
//
//
//
//
//
//
//
//
//
//    public  void variable(Object o, double value) {
////        PPolice pPolice = new PPolice();
//        String blname = String.valueOf(o);  //变量名字是数据库名
//        String svalue = String.valueOf(value);  //查询的报警value值
//        Date data = new Date();
//        Jedis jedis = redisUtil.getJedis();
//
//        List data1 =  excelUtil.excel();
//
//
//        for (int j = 0; j < data1.size(); j++) {
//        if (data1.get(j).equals(o)) {  //数据集合遍历查询跟那个相等
//            String str = (String) data1.get(j);
//
//
//
//
//            // 查询缓存
//            String skuKey = "pol:" + o + ":lock";
//            String skuJson = jedis.get(skuKey);
//            if (StringUtils.isNotBlank(skuJson)) {//相当于if(skuJson!=null&&!skuJson.equals(""))
//
//            } else {
//                String OK = jedis.set("pol:" + o + ":lock", "1", "nx");
//                PUpdate pUpdate1 = updateMapper.selectByVariable1(str);//str是表明 变量名
//                PUpdate pUpdate2 = updateMapper.selectByVariable2(str);
//                PUpdate pUpdate3 = updateMapper.selectByVariable3(str);
//
//                String groups1 = pUpdate1.getGroups();
//                String groups2 = pUpdate2.getGroups();
//                String groups3 = pUpdate3.getGroups();
//
//
//                Double plimit1 = Double.valueOf(pUpdate1.getPlimit()).doubleValue();//报警限值
//                Double plimit2 = Double.valueOf(pUpdate2.getPlimit()).doubleValue();
//                Double plimit3 = Double.valueOf(pUpdate3.getPlimit()).doubleValue();
//
//                if(pUpdate1.getLevels().equals("1")&&value>plimit1){
//                    System.err.println("开启紧急报警: " + "数据库=" + o + " 报警value值=" + value);
//
//                    mapper.insert(blname, "1",
//                            groups1, "1", svalue, pUpdate1.getPlimit(),
//                            pUpdate1.getRecovery(), pUpdate1.getDescribes(), data, "4");
//                }
//                else if(pUpdate2.getLevels().equals("2")&&value>plimit2){
//                    System.err.println("开启重要报警: " + "数据库=" + o + " 报警value值=" + value);
//
//                    mapper.insert(blname, "2",
//                            groups2, "2", svalue, pUpdate2.getPlimit(),
//                            pUpdate2.getRecovery(), pUpdate1.getDescribes(), data, "4");
//                }
//                else if(pUpdate3.getLevels().equals("3")&&value>plimit3){
//                    System.err.println("开启一般报警: " + "数据库=" + o + " 报警value值=" + value);
//
//                    mapper.insert(blname, "3",
//                            groups3, "3", svalue, pUpdate3.getPlimit(),
//                            pUpdate2.getRecovery(), pUpdate1.getDescribes(), data, "4");
//                }
//            }
//
//        }
//
//        }
////        else if ("7S_2A_Speed_Feed".equals(o)){
////            // 查询缓存
////            String skuKey = "pol:" + o + ":lock";
////            String skuJson = jedis.get(skuKey);
////            if (StringUtils.isNotBlank(skuJson)) {//相当于if(skuJson!=null&&!skuJson.equals(""))
////
////            } else {
////                String OK = jedis.set("pol:" + o + ":lock", "1", "nx");
////                if (value>26.11){
////                    System.err.println("开启报警: "+"数据库="+o+" 报警value值="+value);
////                    mapper.insert(blname,"报警类型1紧急报警",
////                            "报警组1","报警级别1紧急",svalue,"26.11",
////                            "20.0","压力过高",data,"当前报警4");
////                }
////            }
////            /*1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111*/
////        }else if ("7S_2A_Speed_Set".equals(o)){
////            // 查询缓存
////            String skuKey = "pol:" + o + ":lock";
////            String skuJson = jedis.get(skuKey);
////            if (StringUtils.isNotBlank(skuJson)) {//相当于if(skuJson!=null&&!skuJson.equals(""))
////
////            } else {
////                String OK = jedis.set("pol:" + o + ":lock", "1", "nx");
////                if (value>26.11){
////                    System.err.println("开启报警: "+"数据库="+o+" 报警value值="+value);
////                    mapper.insert(blname,"报警类型1紧急报警",
////                            "报警组1","报警级别1紧急",svalue,"26.11",
////                            "20.0","压力过高",data,"当前报警4");
////                }
////            }
////
////        }
//
//
//
//
//
//    }
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
////    @RequestMapping("/getTest")
////    public String getTest(){
////        RestTemplate restTemplate=new RestTemplate();
////
////        String url="http://localhost:8086/query?pretty=true&q=select * from cpu&db=bdcloud";
////
////        String points=restTemplate.getForObject(url,String.class);
////
////        System.err.println("points:"+points);
////        return "points:"+points;
////    }
////
////
////    @RequestMapping("/getTests")
////    public String getTests(){
////        RestTemplate restTemplate=new RestTemplate();
////
////        String url="http://localhost:8086/query?pretty=true&q=SELECT * FROM \"7S\" &db=bdcloud";
////
////        String points=restTemplate.getForObject(url,String.class);
////
////        System.err.println("points:"+points);
////        return "points:"+points;
////    }
////
////
////    @RequestMapping("/getWrite")
////    public void writePoints(){
////
////        String url="http://localhost:8086/write?db=bdcloud";
////
////        String pointValue="7S,name=pangkun1,address=shanghai age=21\n" +
////                "7S,name=pangkun2,address=shanghai age=22\n" +
////                "7S,name=pangkun3,address=shanghai age=23";
////
////        RestTemplate restTemplate=new RestTemplate();
////
////        restTemplate.postForObject(url,pointValue,Object.class);
////    }
//
//
//
////    String pointValue="7S_2A_Current_Feed,host=192.168.0.44,value=26.047332787\n" +
////            "7S_2A_Current_Feed,host=192.168.0.44,value=26.1121826406\n" +
////            "7S_2A_Current_Feed,host=192.168.0.44,value=26.1640625234";
//
//
//
//
////    @GetMapping("/queryTestData")
////    public String testQuery() {
////        String command = "select * from cpu";
////        Query query = new Query(command,"bdcloud");
////
////        // QueryResult queryResult = influxDB.query(query);
////        QueryResult queryResult = influxDB.query(query, TimeUnit.MILLISECONDS);
////
////        List<QueryResult.Result> results = queryResult.getResults();
////        if (results == null) {
////            return null;
////        }
////        // 多个sql用分号隔开，因本次查询只有一个sql，所以取第一个就行
////        QueryResult.Result result = results.get(0);
////        List<QueryResult.Series> seriesList = result.getSeries();
////        List<MeasuringPointData> tests = new LinkedList<>();
////
////        for (QueryResult.Series series : seriesList) {
////            if (series == null) {
////                return null;
////            }
////            System.out.println("colums ==>> " + JSON.toJSON(series.getColumns()));
////            System.out.println("tags ==>> " + JSON.toJSON(series.getTags()));
////            System.out.println("name ==>> " + JSON.toJSON(series.getName()));
////            System.out.println("values ==>> " + JSON.toJSON(series.getValues()));
////            System.out.println("查询总数为： ==>> " + (series.getValues() == null ? 0 : series.getValues().size()));
////
////            List<MeasuringPointData> dataVos = new LinkedList<>();
////            series.getValues().forEach(testData -> {
////                MeasuringPointData dataVo = new MeasuringPointData();
////
////                // 直接查询出来的是科学计数法，需要转换为Long类型的数据
////                BigDecimal decimalTime = new BigDecimal(testData.get(0).toString());
////                dataVo.setTime(decimalTime.longValue());
////                dataVo.setAlarm((boolean) testData.get(1));
////                dataVo.setData(testData.get(4).toString());
////                dataVo.setEquipmentId(testData.get(5).toString());
////                dataVo.setLabel(testData.get(6).toString());
////                System.out.println("长度信息==>> " + testData.get(7).toString().length());
////                dataVo.setSystemModelIds(Arrays.asList(StringUtils.split(testData.get(7).toString(), "\\,")));
////                dataVo.setType(Double.valueOf(testData.get(8).toString()).intValue());
////
////                System.out.println("数组长度==>> " +dataVo.getSystemModelIds().size());
////
////                dataVos.add(dataVo);
////            });
////
////            System.out.println("最终结果为： " + JSON.toJSON(dataVos));
////            tests = dataVos;
////        }
////
////        return JSON.toJSONString(tests);
////    }
////
//
//
////    @GetMapping("/he")
////    public String ff(){
////
////        List data =  excelUtil.excel();
////        System.out.println("数据条数="+data.size());
////        System.out.println("集合数据="+data);
////        for (int i = 0; i < data.size(); i++) {
////            System.out.println("数据=  "+ data.get(i));
////            String sql = "select * from "+data.get(i);
////            System.out.println(sql);
////        }
////
////        return "hello";
////    }
//
//
//
//
//
//}
